package org.yoao.crm.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.yoao.crm.DBManager.DBManager;
import org.yoao.crm.bean.AgentResources;

/**
 * @author Weishion.Yi
 * @description
 * @date 2010-10-4 下午03:12:13
 */
public class AgentResourcesDAO extends DBManager {
	private static final Logger log = Logger.getLogger(AgentResourcesDAO.class);

	public int getDataCount(Map<String, String> map) throws Exception {
		String sql = "select count(id) as arsCount from agentResources";
		String where = " where ";
		if (map != null && !map.isEmpty()) {
			Iterator<String> iter = map.keySet().iterator();
			while (iter.hasNext()) {
				String key = iter.next().toString();
				String value = map.get(key).toString();
				where = where + key + "='" + value + "' and ";
			}
		}
		if (where.endsWith(" and "))
			where = where.substring(0, where.length() - 4);
		if (where.equals(" where "))
			where = "";
		sql += where;
		String result = querySingleValue(sql);
		if (result == null || result.trim().equals(""))
			result = "0";
		return Integer.parseInt(result);
	}

	public List<AgentResources> find(int startRow, int endRow, Map<String, String> map) throws Exception {
		String sql = "select top "
				+ endRow
				+ " agentResources.id, agentResources.fileName, agentResources.fileUrl, agentResources.accessSno,"
				+ "agentResources.createTime from agentResources "
				+ "where agentResources.id not in(";
		String filterSql = "select top " + startRow
				+ " a2.id from agentResources as a2 ";

		String where = "";
		String fwhere = "";
		if (map != null && !map.isEmpty()) {
			Iterator<String> iter = map.keySet().iterator();
			while (iter.hasNext()) {
				String key = iter.next().toString();
				String value = map.get(key).toString();
				where = where + "agentResources." + key + "='" + value
						+ "' and ";
				fwhere = fwhere + "a2." + key + "='" + value + "' and ";
			}
		}
		if (where.endsWith(" and "))
			where = where.substring(0, where.length() - 4);
		if (where.equals(""))
			where = null;
		if (fwhere.endsWith(" and "))
			fwhere = fwhere.substring(0, fwhere.length() - 4);
		if (fwhere.equals(""))
			fwhere = null;

		if (where == null) {
			fwhere = " order by a2.id desc";
			sql = sql + filterSql + fwhere
					+ ") order by agentResources.id desc";
		} else {
			filterSql = filterSql + " where " + fwhere + " order by a2.id desc";
			sql = sql + filterSql + ") and " + where
					+ " order by agentResources.id desc";
		}
		return find(sql);
	}

	public AgentResources findById(String id) throws Exception {
		String sql = "select id, rsName, rsPath, accessSno, createTime from agentResources where id="
				+ id;
		return findSingle(querySingleRow(sql));
	}

	private List<AgentResources> find(String sql) throws Exception {
		List list = queryMultiColumn(sql);
		List result = null;
		if (list != null && !list.isEmpty()) {
			result = new ArrayList();
			for (int i = 0; i < list.size(); i++) {
				Map map = (Map) list.get(i);
				AgentResources agentResources = findSingle(map);
				if (agentResources == null)
					agentResources = new AgentResources();
				result.add(agentResources);
			}
		}
		return result;
	}

	private AgentResources findSingle(Map data) throws Exception {
		AgentResources agentResources = null;
		if (data != null && !data.isEmpty()) {
			agentResources = new AgentResources();
			agentResources.setId(data.get("ID").toString());
			agentResources.setRsName((String) data.get("RSNAME"));
			agentResources.setRsPath((String) data.get("RSPATH"));
			agentResources.setAccessSno((String) data.get("ACCESSSNO"));
			agentResources.setCreateTime((String) data.get("CREATETIME"));
		}
		return agentResources;
	}

	public int insert(AgentResources agentResources) throws Exception {
		String sql = "insert into agentResources(rsName, rsPath, accessSno, createTime)values('"
				+ agentResources.getRsName()
				+ "', '"
				+ agentResources.getRsPath()
				+ "', '"
				+ agentResources.getAccessSno()
				+ "', '"
				+ agentResources.getCreateTime() + "')";
		return update(sql);
	}

	public int delete(String id) throws Exception {
		String sql = null;
		if (id.indexOf(",") > 0)
			sql = "delete from agentResources where id in(" + id + ")";
		else
			sql = "delete from agentResources where id=" + id;
		return update(sql);
	}
}